package com.lyl.cloud.controller;

import com.lyl.cloud.common.base.Result;
import com.lyl.cloud.service.HystrixPaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@DefaultProperties(defaultFallback = "defaultFallBack")
public class HystrixController {
    private final HystrixPaymentService hystrixPaymentService;

    @GetMapping("/hystrix/ok/{id:\\d+}")
    @HystrixCommand
    public Result ok(@PathVariable("id") String id){
        System.out.println("hystrix客户端调用 ok方法，id = " + id);
        return hystrixPaymentService.ok(id);
    }

    @GetMapping("/hystrix/timeout/{id:\\d+}")
    @HystrixCommand
    public Result timeout(@PathVariable("id") String id){
        System.out.println("hystrix客户端调用 timeout方法，id = " + id);
        return hystrixPaymentService.timeout(id);
    }

    public Result defaultFallBack(){
        String str = "global fallback方法 80 服务器繁忙或者接口异常 ";
        System.out.println(str);
        return Result.fail(str);
    }
}
